//
//  InfoViewController.m
//  Shanghai Cuisine
//
//  Created by iOS Two on 14/12/17.
//  Copyright (c) 2014年 iOS Two. All rights reserved.
//

#import "InfoViewController.h"
#import "MJRefresh.h"
#import "AnotherViewController.h"

@interface InfoViewController ()

@end

@implementation InfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _i = 0;
    _titleArray = [[NSMutableArray alloc]init];
    _introArray = [[NSMutableArray alloc]init];
    _coverArray = [[NSMutableArray alloc]init];
    _recipeIdArray = [[NSMutableArray alloc]init];
    [self getASIData];
    [self creatController];
    // Do any additional setup after loading the view.
}
-(void)creatController {
    _mainTable = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kwidth, kHeight - stateHight.size.height - navHight) style:UITableViewStylePlain];
    _mainTable.dataSource = self;
    _mainTable.delegate = self;
    _mainTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_mainTable];
    [_mainTable addFooterWithTarget:self action:@selector(getASIData)];
    _mainTable.footerPullToRefreshText = @"上拉可以加载更多数据了";
    _mainTable.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    _mainTable.footerRefreshingText = @"正在加载中";
}

-(void)getASIData {
    NSString *string = @"http://api.hoto.cn/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&deviceid=0F607264FC6318A92B9E13C65DB7CD3C%7C7DB64D6A-18DB-4843-BAD9-0D906C652209%7CC73A6469-0555-4A64-AC37-3A540302FBD0&format=json&loguid=&method=Info.getAlbumInfo&sessionid=1418610742&uuid=353ED12DEEE49FC5F5DF6F2CB3EC5665&vc=31&vn=v4.5.0&aid=";
    string = [string stringByAppendingString:_albumId];
    ASIHTTPRequest *request = [[ASIHTTPRequest alloc]initWithURL:[NSURL URLWithString:[string stringByAppendingString:[NSString stringWithFormat:@"&offset=%d",_i]]]];
    request.username = @"1";
    request.delegate = self;
    [request startAsynchronous];
    _i = _i + 10;
}
-(void)requestFinished:(ASIHTTPRequest *)request {
    SBJsonParser *jsonP = [[SBJsonParser alloc]init];
    if ([request.username isEqualToString:@"1"]) {
        NSDictionary *dic = [[[jsonP objectWithData:[request responseData]] objectForKey:@"result"] objectForKey:@"info"];
        _albumCover = [dic objectForKey:@"AlbumCover"];
        _albumTitle = [dic objectForKey:@"AlbumTitle"];
        _albumContent = [dic objectForKey:@"AlbumContent"];
        NSArray *array = [[[jsonP objectWithData:[request responseData]] objectForKey:@"result"] objectForKey:@"list"];
        for (int i = 0; i < array.count; i++) {
            [_coverArray addObject:[[array objectAtIndex:i] objectForKey:@"Cover"]];
            [_introArray addObject:[[array objectAtIndex:i] objectForKey:@"Intro"]];
            [_recipeIdArray addObject:[[array objectAtIndex:i] objectForKey:@"RecipeId"]];
            [_titleArray addObject:[[array objectAtIndex:i] objectForKey:@"Title"]];
        }
        if (array.count < 10) {
            [_mainTable footerEndRefreshing];
            _mainTable.footerPullToRefreshText = @"没有更多数据了";
            [_mainTable removeFooter];
        } else {
            [_mainTable footerEndRefreshing];
        }
        [_mainTable reloadData];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArray.count + 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return [self stringHight:_albumContent] + 145 + 10;
    } else {
        return [self stringHight:[_introArray objectAtIndex:indexPath.row - 1]] + 183 + 24;
    }
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *string;
    if (indexPath.row == 0) {
        string = @"0";
    } else {
        string = @"1";
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if(cell == nil){
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:string];
    } else {
        for (UIView *view in [cell.contentView subviews]) {
            [view removeFromSuperview];
        }
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (indexPath.row == 0) {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, kwidth, 134)];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        [image setImageURLStr:_albumCover placeholder:[UIImage imageNamed:@"img_newdish"]];
        [cell.contentView addSubview:image];
        
        UILabel *titleName = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kwidth, 20)];
        titleName.center = image.center;
        titleName.text = _albumTitle;
        titleName.textAlignment = NSTextAlignmentCenter;
        titleName.textColor = [UIColor whiteColor];
        titleName.shadowColor = [UIColor blackColor];
        titleName.shadowOffset = CGSizeMake(0, -1.0);
        titleName.font = [UIFont systemFontOfSize:20];
        [cell.contentView addSubview:titleName];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 145, 290, [self stringHight:_albumContent])];
        label.text = _albumContent;
        label.font = [UIFont systemFontOfSize:15];
        label.numberOfLines = 0;
        [cell.contentView addSubview:label];
    } else {
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(15, 0, kwidth - 30, 183)];
        image.clipsToBounds = YES;
        image.contentMode = UIViewContentModeScaleAspectFill;
        [image setImageURLStr:[_coverArray objectAtIndex:indexPath.row - 1] placeholder:[UIImage imageNamed:@"img_newdish"]];
        [image addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageClick:)]];
        image.userInteractionEnabled = YES;
        image.tag = indexPath.row - 1;
        [cell.contentView addSubview:image];
        
        UILabel *titleName = [[UILabel alloc]initWithFrame:CGRectMake(25.5, 150, kwidth-51, 30)];
        titleName.text = [_titleArray objectAtIndex:indexPath.row - 1];
        titleName.textColor = [UIColor whiteColor];
        titleName.font = [UIFont systemFontOfSize:21];
        titleName.shadowColor = [UIColor blackColor];
        titleName.shadowOffset = CGSizeMake(0, -1.0);
        [cell.contentView addSubview:titleName];
        
        UILabel *info = [[UILabel alloc]initWithFrame:CGRectMake(15, 190, 290, [self stringHight:[_introArray objectAtIndex:indexPath.row - 1]])];
        info.text = [_introArray objectAtIndex:indexPath.row - 1];
        info.font = [UIFont systemFontOfSize:15];
        info.numberOfLines = 0;
        [cell.contentView addSubview:info];
    }
    return cell;
}
-(void)imageClick:(UITapGestureRecognizer *)sender {
    AnotherViewController *anther = [[AnotherViewController alloc]init];
    anther.recipeId = [_recipeIdArray objectAtIndex:sender.view.tag];
    self.navigationItem.title = @"返回";
    [self.navigationController pushViewController:anther animated:YES];
}
-(CGFloat)stringHight:(NSString *)string {
    CGSize stringSize = [[NSString stringWithFormat:@"%@",string] sizeWithFont:[UIFont systemFontOfSize:15] constrainedToSize:CGSizeMake(290, 10000) lineBreakMode:NSLineBreakByTruncatingMiddle];
    return stringSize.height;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
